﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 工作日志
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2019-12-17 
    /// </summary>
    public class WorkLogBll
    {
        private WorkLogIService service = new WorkLogService();

        /// <summary>
        /// 列表(我发出的)
        /// </summary>
        /// <param name="requestParam">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<WorkLogEntity>> GetSendList(PageInput input)
        {
            try
            {
                var userId = UserProvider.Instance.Get().UserId;
                return await service.GetSendList(input, userId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表（我收到的）
        /// </summary>
        /// <param name="requestParam">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<WorkLogEntity>> GetReceiveList(PageInput input)
        {
            try
            {
                var userId = UserProvider.Instance.Get().UserId;
                return await service.GetReceiveList(input, userId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<WorkLogEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task Delete(WorkLogEntity entity)
        {
            try
            {
                await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task Create(WorkLogEntity entity)
        {
            try
            {
                entity.Id = CommonHelper.Guid;
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                List<WorkLogShareEntity> workLogShareList = new List<WorkLogShareEntity>();
                var toUserIds = entity.ToUserId.Split(',');
                foreach (var userId in toUserIds)
                {
                    var workLogShare = new WorkLogShareEntity();
                    workLogShare.Id = CommonHelper.Guid;
                    workLogShare.ShareTime = DateTime.Now;
                    workLogShare.WorkLogId = entity.Id;
                    workLogShare.ShareUserId = userId;
                    workLogShareList.Add(workLogShare);
                }
                await service.Create(entity, workLogShareList);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task Update(string id, WorkLogEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                List<WorkLogShareEntity> workLogShareList = new List<WorkLogShareEntity>();
                var toUserIds = entity.ToUserId.Split(',');
                foreach (var userId in toUserIds)
                {
                    var workLogShare = new WorkLogShareEntity();
                    workLogShare.Id = CommonHelper.Guid;
                    workLogShare.ShareTime = DateTime.Now;
                    workLogShare.WorkLogId = entity.Id;
                    workLogShare.ShareUserId = userId;
                    workLogShareList.Add(workLogShare);
                }
                await service.Update(entity, workLogShareList);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
    }
}